package com.yang.studentsapp.ui.friends;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.yang.studentsapp.R;
import com.yang.studentsapp.presenter.IViewSend;
import com.yang.studentsapp.presenter.SettingPresenterImpl;


/**
 * Created by ${Ян-Вэнь-Чао} on 2016/10/10.
 * 添加朋友界面
 */
public class AddFriendsFragment extends Fragment implements View.OnClickListener{

    private EditText editUserName;
    private Button button_add;
    SettingPresenterImpl settingPresenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add_friends, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        settingPresenter = new SettingPresenterImpl();

        editUserName = (EditText) getActivity().findViewById(R.id.et_username);
        button_add = (Button) getActivity().findViewById(R.id.btn_add);
        button_add.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final String name = getText(editUserName);
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(getContext(), "请输入内容...", Toast.LENGTH_SHORT).show();
            return;
        }
        String addReason = getText(editUserName);
       settingPresenter.addContact(name, addReason, new IViewSend() {
           @Override
           public void error(String error) {
               Toast.makeText(getContext(), error, Toast.LENGTH_SHORT).show();
           }

           @Override
           public void start(String msg) {
               Toast.makeText(getContext(),msg, Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onSuccess(String data) {
               Toast.makeText(getContext(),data, Toast.LENGTH_SHORT).show();
           }
       });
    }

    private String getText(EditText editUserName) {
        if (editUserName == null) {
            return null;
        }

        return editUserName.getText().toString().trim();
    }
}
